﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using Newtonsoft.Json;
using Telerik.Web.UI;

namespace RN.ERP.Web.CA
{
    public partial class OrderList : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();

        private void BindData()
        {
            int pageCount = 0;
            int pageSize = 0;
            if (RadGrid1.AllowPaging)
            {
                pageSize = RadGrid1.PageSize;
                RadGrid1.AllowCustomPaging = true;
                pageCount = RadGrid1.CurrentPageIndex;
                RadGrid1.PagerStyle.Visible = true;
                RadGrid1.PagerStyle.AlwaysVisible = true;
            }
            else
            {
                RadGrid1.PagerStyle.Visible = false;
                RadGrid1.PagerStyle.AlwaysVisible = false;
            }
            List<QueryInvoiceInfoCondition> queryList = GetCondition();
            int resultCount = client.GetInvoicesCount(JsonConvert.SerializeObject(queryList));
            List<ModelService.CaPayRelation> result = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetInvoicesList(JsonConvert.SerializeObject(queryList), pageCount, pageSize));
            RadGrid1.VirtualItemCount = resultCount;
            RadGrid1.DataSource = result;
        }

        private void BindDll()
        {
            ddlKind.DataSource = JsonConvert.DeserializeObject<List<ModelService.tb_ProductGroupInfo>>(client.GetProductGroup());
            ddlKind.DataValueField = "Code";
            ddlKind.DataTextField = "GroupName";
            ddlKind.DataBind();
            ddlKind.Items.Insert(0, new ListItem("--请选择--", "-1"));
        }

        private List<QueryInvoiceInfoCondition> GetCondition()
        {
            QueryInvoiceInfoCondition queryCondition = new QueryInvoiceInfoCondition();
            if (ddlKind.SelectedValue != "-1")
            {
                queryCondition.GroupCode = new ParameterValue<string>() { V = ddlKind.SelectedValue, O = OptEnum.Like };
            }
            if (ddlStatus.SelectedValue != "-1")
            {
                if (ddlStatus.SelectedValue == "3.1")
                {
                    queryCondition.bCAStatus = new ParameterValue<byte>() { V = 3, O = OptEnum.Equal };
                    queryCondition.FlowCode = new ParameterValue<string>() { V = "0,019,029,;4000", O = OptEnum.NotEqual };
                }
                else if (ddlStatus.SelectedValue == "3.2")
                {
                    queryCondition.bCAStatus = new ParameterValue<byte>() { V = 3, O = OptEnum.Equal };
                    queryCondition.FlowCode = new ParameterValue<string>() { V = "0,019,029,;4000", O = OptEnum.Equal };
                }
                else
                {
                    queryCondition.bCAStatus = new ParameterValue<byte>() { V = byte.Parse(ddlStatus.SelectedValue), O = OptEnum.Equal };
                }
            }
            if (!string.IsNullOrEmpty(txtCorpName.Text.Trim()))
            {
                queryCondition.CorpName = new ParameterValue<string>() { V = txtCorpName.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtBegin.Text.Trim()) && !string.IsNullOrEmpty(txtEnd.Text.Trim()))
            {
                var date1 = new ParameterValue<DateTime>() { V = DateTime.Parse(txtBegin.Text.Trim()), O = OptEnum.GreaterThanOrEqual };
                var date2 = new ParameterValue<DateTime>() { V = DateTime.Parse(txtEnd.Text.Trim()).AddDays(1), O = OptEnum.LessThan };
                var list = new List<ParameterValue<DateTime>>();
                list.Add(date1);
                list.Add(date2);
                queryCondition.RegisterDate = new ParameterValues<DateTime>()
                {
                    Logic = LogicEnum.And,
                    Values = list
                };
            }
            else if (!string.IsNullOrEmpty(txtBegin.Text.Trim()))
            {
                queryCondition.RegisterDate1 = new ParameterValue<DateTime>() { V = DateTime.Parse(txtBegin.Text.Trim()), O = OptEnum.GreaterThanOrEqual };
            }
            else if (!string.IsNullOrEmpty(txtEnd.Text.Trim()))
            {
                queryCondition.RegisterDate1 = new ParameterValue<DateTime>() { V = DateTime.Parse(txtEnd.Text.Trim()).AddDays(1), O = OptEnum.LessThan };
            }
            if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                queryCondition.Code = new ParameterValue<string>() { V = txtCode.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(ucArea.AreaCode))
            {
                queryCondition.CurrentAreaCode = new ParameterValue<string>() { V = ucArea.AreaCode, O = OptEnum.StartWith };
            }
            if (ddlProCode.SelectedValue != "-1")
            {
                queryCondition.ProCode = new ParameterValue<string>() { V = ddlProCode.SelectedValue, O = OptEnum.Like };
            }
            List<QueryInvoiceInfoCondition> queryList = new List<QueryInvoiceInfoCondition>();
            queryList.Add(queryCondition);
            return queryList;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDll();
            }
            if (!string.IsNullOrEmpty(ucArea.AreaCode))
            {
                ucArea.AreaCode = ucArea.AreaCode;
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void RadGrid1_temDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
            string str = "";
            int index = 0;
            int intRowSpan = 1;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    Label lbId = (Label)RadGrid1.Items[i].Cells[3].FindControl("lbId");
                    str = lbId.Text;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        Label lbId2 = (Label)RadGrid1.Items[j].Cells[3].FindControl("lbId");
                        if (lbId2.Text == str)
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[16].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[16].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }

            if (e.Item.ItemType == GridItemType.Footer)
            {
                e.Item.Cells[2].Text = "<b>合计</b>";
                e.Item.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                List<QueryInvoiceInfoCondition> queryList = GetCondition();
                List<ModelService.CaPayRelation> result = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetInvoicesList(JsonConvert.SerializeObject(queryList), 0, 0));
                e.Item.Cells[14].Text = "<b>" + float.Parse(result.Sum(m => m.SumPay).ToString()).ToString("F") + "</b>";
                e.Item.Cells[14].HorizontalAlign = HorizontalAlign.Center;
            }
        }

        protected void lbSearch_Click(object sender, EventArgs e)
        {
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }

        protected void lbExport_Click(object sender, EventArgs e)
        {
            RadGrid1.AllowPaging = false;
            BindData();
            RadGrid1.DataBind();
            RadGrid1.ExportSettings.FileName = "List";
            RadGrid1.ExportSettings.ExportOnlyData = true;
            RadGrid1.MasterTableView.ExportToExcel();
        }
    }
}